export default {
  data() {
    return {
      rules: []
    }
  },
  methods: {
    validate() {
      return new Promise((resolve, reject) => {
        let rules = this.rules
        let parameter = this.parameter
        let success = true
        let errorMsg = ''
        let len = rules.length
        for (let i = 0; i < len; i++) {
          let item = rules[i]
          let v = parameter[item.key]
          if (typeof v == 'string') {
            v = v.trim()
          }
          if (v === void 0) {
            //rules中的项必须对应parameter
            throw new Error('you have rule item but not in parameter witch rule key is "' + item.key + '"')
          }
          if (
            (item.value !== void 0 && v !== item.value) ||
            (item.require === true && (v === '' || v === null || v === void 0))
          ) {
            errorMsg = item.message
            success = false
            break
          }
        }
        if (success) {
          resolve(parameter)
        } else {
          this.$toast({
            message: errorMsg,
            position: 'bottom'
          })
        }
      })
    },
  }
}